#include "Texture.h"

Texture* Texture::createTextureFromFilePath(LPDIRECT3DDEVICE9 d3ddv, LPWSTR file, int Width, int Height, int Count, int PerRow) {
	Texture *texture;	
	HRESULT result;

	texture = new Texture(Width, Height, Count, PerRow);
	result = D3DXGetImageInfoFromFile(file, &texture->Info);
	if (result != D3D_OK) {
		return NULL;
	}

	result = D3DXCreateTextureFromFileEx(
		d3ddv,
		file,
		texture->Info.Width,
		texture->Info.Height,
		1,
		D3DUSAGE_DYNAMIC,
		D3DFMT_UNKNOWN,
		D3DPOOL_DEFAULT,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		NULL,//D3DCOLOR_ARGB(255, 255, 255, 255),
		&texture->Info,
		NULL,
		&texture->Image
		);

	if (result != D3D_OK) {
		return NULL;
	}

	// Check perRow is zero, caculate by devide width / info width
	if (texture->PerRow == 0) {
		texture->PerRow = texture->Info.Width / texture->Width;
		texture->PerHeight = texture->Info.Height / texture->Height;
		texture->Count = texture->PerRow * (texture->Info.Height / texture->Height);
	}

	return texture;
}

Texture::Texture(int Width, int Height, int Count, int PerRow) {
	Image = NULL;

	this->Width = Width;
	this->Height = Height;
	this->Count = Count;
	this->PerRow = PerRow;

	pCenter = new D3DXVECTOR3();
}

void Texture::destroy() {
	Image->Release();
}

D3DXVECTOR3* Texture::getPosCenter(double x, double y) {
	pCenter->x = (double)Width/2.0f;
	pCenter->y = (double)Height/2.0f;
	//pCenter->z = 0;

	return pCenter;
}